#define  _CRT_SECURE_NO_WARNINGS 1
#include "Stack.h"

void StackInit(Stack* ps)
{
	assert(ps);

	ps->arr = NULL;
	ps->capacity = 0;
	ps->top = 0;
}

void StackDestory(Stack* ps)
{
	assert(ps);

	free(ps->arr);
	ps->arr = NULL;
	ps->top = 0;
	ps->capacity = 0;
}

void StackCheakCapacity(Stack* ps)
{
	int newcapacity = ps->capacity == 0 ? 5 : 2 * (ps->capacity);
	STDataType* pn = (STDataType*)realloc(ps->arr, sizeof(STDataType) * newcapacity);
	if (pn == NULL)
	{
		perror("realloc");
		return;
	}
	ps->arr = pn;
	ps->capacity = newcapacity;
}


void StackPush(Stack* ps, STDataType x) 
{
	assert(ps);

	if (ps->top == ps->capacity)
		StackCheakCapacity(ps);

	ps->arr[ps->top] = x;
	ps->top++;
}

void StackPop(Stack* ps)
{
	assert(ps);
	assert(ps->top > 0);

	ps->top--;
}

STDataType StackTop(Stack* ps)
{
	assert(ps);
	assert(ps->top > 0);

	return ps->arr[ps->top - 1];
}

int StackSize(Stack* ps)
{
	assert(ps);

	return ps->top;
}

bool StackEmpty(Stack* ps)
{
	assert(ps);

	return ps->top == 0;
}